import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from "vuex-persistedstate"

Vue.use(Vuex)
const createStorage = createPersistedState({
  key: 'vuexStoragePad',
  storage: window.localStorage,
  reducer: (state) => ({
    access_token: state.access_token,
    userInfo: state.userInfo,
    checkWord: state.checkWord,
    loginModalVisible: state.loginModalVisible,
    forgetModalVisible: state.forgetModalVisible,
  })
})

const globleModal = createPersistedState({
  key: 'globleModal',
  storage: window.sessionStorage,
  reducer: (state) => ({
    translateModalVisible: state.translateModalVisible,
    grammarSkillModal: state.grammarSkillModal,
    grammarExplainVisible: state.grammarExplainVisible,
    readBroadModalVisible: state.readBroadModalVisible,
  })
})

const createReverseWord = createPersistedState({
  key: 'reverseWrod',
  storage: window.localStorage,
  reducer: (state) => ({
    reverseAudioSrc: state.reverseAudioSrc,
    sentenceCurrent: state.sentenceCurrent,
    reverseStatus: state.reverseStatus,
    sentenceList: state.sentenceList,
    grammarBlankAnswer: state.grammarBlankAnswer,
  })
})

const createSuperReadStorage = createPersistedState({
  key: 'superRead',
  storage: window.sessionStorage,
  reducer: (state) => ({
    readCurrent: state.readCurrent,
    readCourseInfo: state.readCourseInfo,
    readDetail: state.readDetail,
    readReport: state.readReport,
  })
})


export default new Vuex.Store({
  state: {
    work_end_time:{time:0},
    after_out_time:{
     time:0,mtime:0},
    sentenceCurrent: {},
    checkWord: [],
    access_token: '',
    userInfo: {},
    reverseAudioSrc: '',
    reverseStatus: {},
    sentenceList: {
      list: [],
      test: {}
    },
    readCourseInfo: {},
    readCurrent: {},
    readReport: {},
    readDetail: {},
    grammarSkillModal: {},
    grammarExplainVisible: false,
    grammarBlankAnswer: {
      visible: false,
      id: ''
    },
    translateModalVisible: {
      visible: false,
    },
    forgetModalVisible: {visible: false},
    readBroadModalVisible: {}
  },
  mutations: {
    changeWorkTime(state, params) {
      state.work_end_time.time = params
    },
    setAfterOutTime(state, params) {
      state.after_out_time.time = params.time
      state.after_out_time.mtime = params.mtime
    },
    addCheckWord(state, params) {
      state.checkWord = params
      console.log(state)
    },
    setLogin(state, params) {
      state.access_token = params.access_token,
      state.userInfo = params.userInfo
    },
    setLoginModal(state, visible) {
      state.loginModalVisible = visible;
    },
    setForgetModal(state, info) {
      state.forgetModalVisible = info
    },
    setTranslateModal(state, data) {
      state.translateModalVisible = data
    },
    setReadBroadModal(state, data) {
      state.readBroadModalVisible = data
    },
    setGrammarExplain(state, visible) {
      state.grammarExplainVisible = visible
    },
    setGrammarSkill(state, data) {
      state.grammarSkillModal = data
    },
    setGrammarBlankAnswer(state, data) {
      state.grammarBlankAnswer = data
    },
    setReverseAudio(state, src) {
      state.reverseAudioSrc = src
    },
    setReverseStatus(state, params) {
      state.reverseStatus = params || {}
    },
    setSentenceList(state, params) {
      state.sentenceList = params || {}
    },
    setSentenceCurrent(state, params) {
      state.sentenceCurrent = params || {}
    },
    setReadCourseInfo(state, params) {
      state.readCourseInfo = params || {}
    },
    setReadCurrent(state, params) {
      state.readCurrent = params || {}
    },
    setReadDetail(state, params) {
      state.readDetail = params || {}
    },
    setReadReport (state, params) {
      state.readReport = params
    }
  },
  actions: {
    setCheckWord({
      commit
    }, params) {
      commit('addCheckWord', params)
    },
  },
  modules: {},
  plugins: [createStorage, createReverseWord, createSuperReadStorage, globleModal]
})